package cultivameCore.utils;

public class Math {
	
	public static float pow(float b,float ex) {
		return (float) exp((float) (ex*log(b)));
	}

	public static float exp(float d) {
		// TODO Auto-generated method stub
		float sum=1;
		for (int i=1;i<10;i++){
			sum+=pow(d,i)/fact(i);
		}
		return sum;
	}

	public static float log(float b, float n) {
		return (float) (log(n)/log(b));		
	}
	
	public static float pow(float base, int exp){
	    if(exp == 0) return 1;
	    float res = base;
	    for(;exp > 1; --exp)
	        res *= base;
	    return res;
	}

	public static double log(double x) {
	    long l = Double.doubleToLongBits(x);
	    long exp = ((0x7ff0000000000000L & l) >> 52) - 1023;
	    double man = (0x000fffffffffffffL & l) / (double)0x10000000000000L + 1.0;
	    double lnm = 0.0;
	    double a = (man - 1) / (man + 1);
	    for( int n = 1; n < 7; n += 2) {
	        lnm += pow((float) a, n) / n;
	    }
	    return 2 * lnm + exp * 0.69314718055994530941723212145818;
	}
	
	public static float fact(int n){
		if(n<=2){
			return n;
		}else{
			return n*fact(n-1);
		}
	}
	
	public static int round(float n){
		float aux=n -(int)(n);
		if(aux>=0.5){
			return (int)n+1;
		}else{
			return (int)n;
		}
	}
	
	public static float abs(float n){
		if(n<0){
			return -n;
		}else{
			return n;
		}
	}
}
